package cn.cqnu.service.impl;

import cn.cqnu.domin.entity.Manager;
import cn.cqnu.domin.entity.Student;
import cn.cqnu.domin.vo.ResponseResult;
import cn.cqnu.global.SystemReturnEnum;
import cn.cqnu.mapper.ManagerMapper;
import cn.cqnu.service.ManagerService;
import cn.cqnu.utils.JWTUtils;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static cn.cqnu.global.SystemConstant.TYPE_ADMIN;
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Override
    public ResponseResult login(Long managerId, String password) {
        // 密码加密
        password = SecureUtil.md5(password);

        // 比对
        LambdaQueryWrapper<Manager> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Manager::getManagerId,managerId).eq(Manager::getManagerPassword,password);
        Manager res = getOne(wrapper);

        if(res != null){
            return ResponseResult.error(SystemReturnEnum.LOGIN_INFO_WRONG);
        }
        // 颁发jwt
        String token = JWTUtils.generateJWT(managerId, TYPE_ADMIN);

        HashMap<String, String> data = new HashMap<>();
        data.put("token", token);

        return ResponseResult.ok(data);
    }
}
